package com.tianpengtech.front.web.fair;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairext.FairExt;
import com.tianpengtech.common.model.fairphoto.FairPhoto;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.task.TaskWh;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.NotEqualCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.front.mobile.passport.MobilePassportService;
import com.tianpengtech.front.web.passport.PassportService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.DoubleAccumulator;

/**
 * Created by Administrator on 2017/6/4 0004.
 */
public class FairService {
    private Fair fairDao = Fair.dao;

    public static final String PARAM_NICKNAME ="nickName"; //昵称
    public static final String PARAM_PROVONCE ="province"; //省
    public static final String PARAM_CITY ="city"; //市
    public static final String PARAM_PLATFORM ="platform"; //平台
    public static final String PARAM_CHANNEL ="channel"; //渠道


    /**
     * 分页查询
     * @param pageNum
     * @param length
     * @param params
     * @return
     */
    public Page<Fair> fair(int pageNum, int length, Map<String,Object> params){
        String sql="select * ";
        QueryConstructor queryConstructor = new QueryConstructor();
        queryConstructor.setCommand("from "+fairDao.tableName());
        queryConstructor.and(new EqualCondition(Fair.DELETETAG, Fair.DELETE_STATUS_NO));
        queryConstructor.and(new EqualCondition(Fair.STATUS, Fair.STATUS_SUCCESS));
        if (params != null) {
            Object nickName = params.get(PARAM_NICKNAME);
            Object province = params.get(PARAM_PROVONCE);
            Object city = params.get(PARAM_CITY);
            Object platform = params.get(PARAM_PLATFORM);
            Object channel = params.get(PARAM_CHANNEL);
            Object feature = params.get(Fair.FANS_FEATURE);
            Object price = params.get(Fair.PRICE);
            if (StrKit.notBlank(nickName.toString())){
                queryConstructor.and(new LikeCondition(Fair.NICK_NAME,"%"+nickName.toString()+"%"));
            }
            if (StrKit.notBlank(province.toString())){
                queryConstructor.and(new EqualCondition(Fair.PROVINCE,province.toString()));
            }
            if (StrKit.notBlank(city.toString().trim())){
                queryConstructor.and(new EqualCondition(Fair.CITY,city.toString()));
            }
            if (StrKit.notBlank(platform.toString().trim())){
                queryConstructor.and(new LikeCondition(Fair.PLATFORM, "%"+platform.toString()+"%"));
            }
            if (StrKit.notBlank(channel.toString().trim())){
                queryConstructor.and(new LikeCondition(Fair.CHANNEL,"%"+channel.toString()+"%"));
            }
            if (StrKit.notBlank(feature.toString())){
                queryConstructor.and(new LikeCondition(Fair.FANS_FEATURE,"%"+feature.toString()+"%"));
            }
            
            if (StrKit.notBlank(price.toString())){
                queryConstructor.and(new EqualCondition(Fair.PRICE,price));
            }
        }
        String from = queryConstructor.buildSQL();
//        Page<Fair> page=fairDao.paginateByCache(Fair.FAIR_CACHE_NAME,params.toString()+"_"+pageNum+"_"+length,pageNum,length,sql,from,queryConstructor.getParams().toArray());
        Page<Fair> page=fairDao.paginate(pageNum,length,sql,from,queryConstructor.getParams().toArray());
        List<Fair> fairs = page.getList();
        for (Fair f:fairs) {
            f.put("attrString",f.getAttrString());
            f.put("click",f.getClick());
            f.put("cvs",f.getCvs());
        }
        return page;
    }


    /**
     * 根据ID判断网红存在不存在
     * @param ids
     * @return
     */
    public boolean isExistFair(String[] ids){
        boolean f=true;
        for (String id:ids) {
            Fair fair=fairDao.findFirstByCache(Fair.FAIR_CACHE_NAME,"fair_"+id,"select id from "+fairDao.tableName() +" where id= ?",id);
            if (fair==null) {
                f=false;
            }
        }
        return f;
    }

    /**
     * 根据id获取网红信息
     * @param id
     * @return
     */
    public Fair findById(int id){
//        return fairDao.findFirstByCache(Fair.FAIR_CACHE_NAME,"fair_info_"+id,"select * from "+Fair.dao.tableName()+" where " +Fair.DELETETAG + "=? and "+Fair.ID+"=?",0,id);
        Fair  fair  = fairDao.findFirst("select * from "+Fair.dao.tableName()+" where " +Fair.DELETETAG + "=? and "+Fair.ID+"=?",0,id);
//        String sql="select * from "+FairExt.dao.tableName()+" where wh_id=? and type=?";
//		List<FairExt> xqah = FairExt.dao.find(sql,id,FairExt.TYPE_XQAH);
//		fair.put("xqah",xqah);
//		List<FairExt> honner = FairExt.dao.find(sql,id,FairExt.TYPE_HONNER);
//		fair.put("honner",honner);
		
//		sql="select * from "+FairPhoto.dao.tableName()+" where wh_id=? and deletetag=0 and is_photo=1";
//		List<FairPhoto> photo = FairPhoto.dao.find(sql,id);
//		fair.put("photo",photo);
		return fair;
    }

    
    public Fair findFairDetailById(int id){
    	
    	Fair  fair  = fairDao.findFirst("select * from "+Fair.dao.tableName()+" where " +Fair.DELETETAG + "=? and "+Fair.ID+"=?",0,id);
        String sql="select * from "+FairExt.dao.tableName()+" where wh_id=? and type=?";
		List<FairExt> xqah = FairExt.dao.find(sql,id,FairExt.TYPE_XQAH);
		fair.put("xqah",xqah);
		List<FairExt> honner = FairExt.dao.find(sql,id,FairExt.TYPE_HONNER);
		fair.put("honner",honner);
		
		sql="select * from "+FairPhoto.dao.tableName()+" where wh_id=? and deletetag=0 and is_photo=1";
		List<FairPhoto> photo = FairPhoto.dao.find(sql,id);
		fair.put("photo",photo);
		return fair;
    }
    

    /**
     * 根据任务ID获取相关所有网红信息
     * @param taskId
     * @return
     */
    public List<Fair> findByTask(String taskId){
        List<Fair> fairs=fairDao.find("select fair.*,tw.*  from "+ TaskWh.dao.tableName()+" tw left join "+Fair.dao.tableName()+" fair on fair."+Fair.ID+"=tw."+TaskWh.WH_ID+" where tw."+TaskWh.DELETETAG+"=0 and "+TaskWh.TASK_ID +"=?",taskId);
        return fairs;
    }

    /**
     * 根据任务ID获取相关所有网红信息
     * @param taskId
     * @return
     */
    public List<Fair> findByTaskJob(String taskId){
        List<Fair> fairs=fairDao.find("select fair.* from "+ TaskWh.dao.tableName()+" tw left join "+Fair.dao.tableName()+" fair on fair."+Fair.ID+"=tw."+TaskWh.WH_ID+" where tw."+TaskWh.DELETETAG+"=0 and "+TaskWh.TASK_ID +"=?",taskId);
        return fairs;
    }

    /**
     * 根据任务ID获取白名单的网红信息
     * @param taskId
     * @return
     */
    public List<Record> findByWhite(String taskId){
        List<Record>  fairs=Db.findByCache(Task.TASK_WHITELIST_CACHE_NAME,Task.TASK_WHITELIST+"_"+taskId,"select fair.* from "+Task.TASK_WHITELIST+" wl left join "+Fair.dao.tableName()+" fair on wl."+Task.WL_FAIR_ID+"=fair."+Fair.ID + " where wl."+Task.WL_TASK_ID+"=?",new Object[]{taskId});
        return fairs;
    }
    
    
    public Fair findByMobile(String mobile){
    	String sql = "select * from "+Fair.dao.tableName()+" where "+Fair.MOIBILE+"=? limit 1 ";
    	return Fair.dao.findFirst(sql,mobile);
    }

}
